package com.protocols.sctp;
import java.rmi.*;

import java.rmi.registry.*;

import java.rmi.server.*;
import java.util.Arrays;
import java.util.Random;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;

import com.protocols.GeneralProtocolData;
import com.protocols.ProtoOperations;
import com.protocols.sctp.m3ua.M3uaASPAC;
import com.protocols.sctp.m3ua.M3uaHandler;
import com.protocols.sctp.m3ua.M3uaHandlerInterface;


 

public class SctpServerHandler extends java.rmi.server.UnicastRemoteObject implements SctpHandlerInterface{

	static Socket serverSocket ;
	static DataOutputStream outToClient ;
	static DataInputStream inFromClient;
    static Registry Registry;  
    static Logger Log = Logger.getLogger("SCTP");
   
	  
    protected SctpServerHandler() throws RemoteException {
		super();
		Log.info("Started SctpHandler");
	}

    

	public static void main(String[] args) {

		try{
			// Configuring the Logger
			FileHandler filehandler= new FileHandler("F:\\Projects\\Google\\mihira\\logs\\sctp.txt");
			filehandler.setFormatter(new SimpleFormatter());
			Log.addHandler(filehandler);
			Log.setLevel(Level.ALL);			
			Log.info("Logger Initialised");
			
			Registry = LocateRegistry.createRegistry( 9001 );
			Log.info("Registry bound to port :"+9001);
			Registry.rebind("SctpHandler", new SctpServerHandler());
			
			while(true){
				
			
				ServerSocket welcomeSocket = new ServerSocket(6789);
				serverSocket = welcomeSocket.accept();
		       
			     
				outToClient = new DataOutputStream(serverSocket.getOutputStream());
				inFromClient = new DataInputStream(serverSocket.getInputStream());
				Log.info("Sctp Client bound to .....");  		
				
				
				Registry.rebind("M3uaHandler", new M3uaHandler());	// Starting the M3ua Layer
				M3uaHandlerInterface M3uaHandler=(M3uaHandlerInterface)(Registry.lookup("M3uaHandler"));
	    		
				
				byte []input =new byte[300];
				try{
					while ( true){
						  int length=inFromClient.read(input);
						  
						  if(length>0){
							  Log.info("Byte Stream received : "+ProtoOperations.getHexString(input));
							  Log.info("Creating the Treatment Thread");
							  new TreatInCommingTcp(ProtoOperations.extractByteArray(input, 0, length), M3uaHandler, outToClient, new Random().nextInt(1000000000)).start();
						  }
							  
						  
					  }
				}catch(SocketException s){
					
				}
			}
		  
		}catch(Exception e){
			Log.warning("Exception Occured :" +e.getClass().getSimpleName()+":"+ Arrays.toString(e.getStackTrace()));

		}
    }
    

	@Override
	public void m3uaToSctp(long sessionid,GeneralProtocolData m3uamsg) throws RemoteException {
		try{
			byte [] tosend=((GeneralProtocolData)m3uamsg).getByteArray();
			outToClient.write(tosend);
		}catch(Exception e){
			
		}
		
	}


	

   
   

   
}